import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { routerTransition } from '../router.animations';
import { LoginService } from './common-service/login.service';
import { AppSettings, UserStatus, ConstantVariable } from '../shared/config/app-settings';
import { Message } from 'primeng/api';
import * as _ from 'lodash';
import { FileUpload } from 'primeng/fileupload';
import { AttachmentUtilsService } from '../shared/services/attachment-utils.service';
import { UserService } from '../shared/services/user.service';
import { DeviceDetectorService } from 'ngx-device-detector';
@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss'],
    animations: [routerTransition()]
})
export class LoginComponent implements OnInit {

    code: string;
    password: string;
    msgs: Message[] = [];
    constructor(public router: Router,
        private loginService: LoginService,
        private userService: UserService,
        private deviceService: DeviceDetectorService,
        private attachmentUtilsService: AttachmentUtilsService) { }

    ngOnInit() { }

    isDesktop(){
        return this.deviceService.isDesktop();
      }

    onLoggedin(role?) {
        localStorage.setItem('isLoggedin', 'true');
        // if (AppSettings.mode === 'web') {
        //     return;
        // }
        // if(role === 'admin'){
        //     this.router.navigate(['/admin']);
        // }else if(role === 'customer'){
        //     this.router.navigate(['/customer']);
        // }else if(role === 'agent'){
        //     this.router.navigate(['/agent']);
        // }
        this.userService.loginUser(this.code, this.password).subscribe(
            (response) => {
                let role = response.data.role;
                this.gotoUrlByRole(role);
            },
            (error) => {
                this.showError(error.error.msg);
                console.log('POST call in error', error);
            }
        );
    }


    showInfo(message: string) {
        this.msgs.push({ severity: 'info', summary: 'Message', detail: message });
    }

    showError(message: string) {
        this.msgs.push({ severity: 'error', summary: 'Message', detail: message });
    }

    gotoUrlByRole(role){
         if(role === 'admin'){
            this.router.navigate(['/admin']);
        }else if(role === 'customer'){
            this.router.navigate(['/customer']);
        }else if(role === 'agent'){
            this.router.navigate(['/agent']);
        }
    }





}
